import java.net.*;
import java.io.*;

public class Player implements Runnable
{  private Socket socket              = null;
   private Thread thread              = null;
   private DataInputStream  console   = null;
   private DataOutputStream streamOut = null;
   private PlayerThread client    	  = null;

   public Player(String serverName, int serverPort)
   {  System.out.println("Establishing connection. Please wait ...");
      try
      {  socket = new Socket(serverName, serverPort);
         System.out.println("Connected: " + socket);
         start();
      }
      catch(UnknownHostException uhe)
      {  System.out.println("Host unknown: " + uhe.getMessage()); }
      catch(IOException ioe)
      {  System.out.println("Unexpected exception: " + ioe.getMessage()); }
      
      sendMessage("howdy!");
   }
   //function that sends commands to the server
   public void run()
   {  while (thread != null)
      {  try
         {  streamOut.writeUTF(console.readLine());
            streamOut.flush();
         }
         catch(IOException ioe)
         {  System.out.println("Sending error: " + ioe.getMessage());
            stop();
         }
      }
   }
   public void handle(String msg)
   {  if (msg.equals(".bye"))
      {  System.out.println("Good bye. Press RETURN to exit ...");
         stop();
      }
      else if(msg.equals("start")){
	   
   }
      else
         System.out.println(msg);
   }
   public void start() throws IOException
   {  console   = new DataInputStream(System.in);
      streamOut = new DataOutputStream(socket.getOutputStream());
      if (thread == null)
      {  client = new PlayerThread(this, socket);
         thread = new Thread(this);                   
         thread.start();
      }
   }
   
   public void sendMessage(String message){
	   try{  
		   streamOut.writeUTF(message);
           streamOut.flush();
       }
       catch(IOException ioe){  
    	   System.out.println("Sending error: " + ioe.getMessage());
           stop();
       }
   }
   
   public void sendInt(int x, int y){
	   try{  
		   streamOut.writeInt(x);
		   streamOut.writeInt(y);
           streamOut.flush();
       }
       catch(IOException ioe){  
    	   System.out.println("Sending error: " + ioe.getMessage());
           stop();
       }
   }
   
   
   public void stop()
   {  if (thread != null)
      {  thread.stop();  
         thread = null;
      }
      try
      {  if (console   != null)  console.close();
         if (streamOut != null)  streamOut.close();
         if (socket    != null)  socket.close();
      }
      catch(IOException ioe)
      {  System.out.println("Error closing ..."); }
      client.close();  
      client.stop();
   }
   public static void init() 
   {  Player client = null;
      //if (args.length != 2)
      //   System.out.println("Usage: java ChatClient host port");
      //else
      //   client = new Player(args[0], Integer.parseInt(args[1]));
   	  client = new Player("127.0.0.1", 9001);
   }
}